import { b2CollideEdgeAndCircle, b2Contact, b2ContactEdge, b2ContactImpulse, b2ContactListener, b2EdgeShape, b2Manifold, b2Shape } from "@box2d/core";
import { BitmapRenderer } from "./engine/BitmapRenderer";
import { GameObject } from "./engine/GameObject";
import { System } from "./engine/System";
import { RigidBody } from "./RigidBody";
import { Transform } from "./engine/Transform";
import { LifeCollider } from "./LifeCollider";


export class GamePlaySystem extends System implements b2ContactListener {

    onJump: boolean = false;

    private prefabGameObject: GameObject;


    onStart() {

        var speedXl: number = 0
        var speedXr: number = 0
        var speedY: number = 0
        var tl = 0;
        var tr = 0;
        var slow: number

        window.addEventListener('keydown', () => {
            const mainRole = this.engine.getGameObject('player');
            if (mainRole) {
                const rigid = mainRole.getBehaviour(RigidBody);
                var e = event || window.event || arguments.callee.caller.arguments[0];
                if (e && e.keyCode == 65) { // 按 a 
                    speedXl = -2.5;
                }
                if (e && e.keyCode == 68) { // 按 d 
                    speedXr = 2.5;
                }
                if (e && e.keyCode == 32) { // 按 空格
                    //speedY = 5;
                    if (this.onJump) {
                        speedY = 8;
                        if (mainRole) {
                            const rigid = mainRole.getBehaviour(RigidBody);

                        }
                        rigid.b2body.SetLinearVelocity({ x: speedXl + speedXr, y: speedY });
                    }
                    else {
                        //speedY = 0
                        return;
                    }
                }

                slow = rigid.b2body.GetLinearVelocity().y;
                rigid.b2body.SetLinearVelocity({ x: speedXl + speedXr, y: slow });

                if (speedY != 0) {
                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/jump.png"
                    if (speedXl != 0) {
                        if (mainRole.getBehaviour(Transform).scaleX == 1) {
                            mainRole.getBehaviour(Transform).scaleX = -1;
                        }
                    }
                    if (speedXr != 0) {
                        if (mainRole.getBehaviour(Transform).scaleX == -1) {
                            mainRole.getBehaviour(Transform).scaleX = 1;
                        }
                    }
                }
                else {
                    if (speedXl != 0 || speedXr != 0) {
                        if (speedXl != 0 && speedXr != 0) {
                            mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk1.png"
                        }
                        else if (speedXl != 0) {
                            if (tl < 24) {
                                tl++;
                            }
                            else {
                                tl = 1;
                            }
                            var tlz = (tl - 1) / 4
                            tlz = Math.floor(tlz)
                            switch (tlz) {
                                case 0: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk2.png";
                                    break;
                                }
                                case 1: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk3.png";
                                    break;
                                }
                                case 2: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk4.png";
                                    break;
                                }
                                case 3: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk5.png";
                                    break;
                                }
                                case 4: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk6.png";
                                    break;
                                }
                                case 5: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk1.png";
                                    break;
                                }
                            }
                            if (mainRole.getBehaviour(Transform).scaleX == 1) {
                                mainRole.getBehaviour(Transform).scaleX = -1;
                            }

                        }
                        else if (speedXr != 0) {
                            if (tr < 24) {
                                tr++;
                            }
                            else {
                                tr = 1;
                            }
                            var trz = (tr - 1) / 4
                            trz = Math.floor(trz)
                            switch (trz) {
                                case 0: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk2.png";
                                    break;
                                }
                                case 1: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk3.png";
                                    break;
                                }
                                case 2: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk4.png";
                                    break;
                                }
                                case 3: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk5.png";
                                    break;
                                }
                                case 4: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk6.png";
                                    break;
                                }
                                case 5: {
                                    mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk1.png";
                                    break;
                                }
                            }
                            if (mainRole.getBehaviour(Transform).scaleX == -1) {
                                mainRole.getBehaviour(Transform).scaleX = 1;
                            }
                        }
                    }
                    else {
                        mainRole.getBehaviour(BitmapRenderer).source = "assets/rockman/walk1.png"
                    }
                }

            }
            else {
                return
            }
        })

        window.addEventListener('keyup', () => {
            const mainRole = this.engine.getGameObject('player');
            if (mainRole) {
                const rigid = mainRole.getBehaviour(RigidBody);
                var e = event || window.event || arguments.callee.caller.arguments[0];
                if (e && e.keyCode == 65) { // 按 a 
                    speedXl = 0;
                }
                if (e && e.keyCode == 68) { // 按 d 
                    speedXr = 0;
                }
                if (e && e.keyCode == 32) { // 按 空格
                    speedY = 0;
                    return;
                }
                slow = rigid.b2body.GetLinearVelocity().y;
                rigid.b2body.SetLinearVelocity({ x: speedXl + speedXr, y: slow });
            }
            else {
                return
            }
        })

        const sceneRootGameObject = this.engine.getGameObject('sceneRoot');
        const prefabGameObject1 = this.engine.createPrefab("menu.json");
        sceneRootGameObject.addChild(prefabGameObject1);

        const uiRoot = this.engine.getGameObject("uiRoot");
        const prefabGameObject2 = this.engine.createPrefab('ui.json');
        uiRoot.addChild(prefabGameObject2);

    }

    BeginContact(contact: b2Contact<b2Shape, b2Shape>): void {

        const mainRole = this.engine.getGameObject('player');
        const rigid = mainRole.getBehaviour(RigidBody);

        const obj = contact.GetFixtureA()
        const obj2 = contact.GetFixtureB()
        const shape = obj.GetShape().GetType();

        //console.log(obj)

        if (shape == 1) {
            this.onJump = true;
        }
        if (obj.IsSensor() == true) {
            const does = obj.GetBody().GetUserData()
            does.getBehaviour(LifeCollider).onSensor()
        }
        if (obj2.IsSensor() == true) {
            const does = obj2.GetBody().GetUserData()
            does.getBehaviour(LifeCollider).onSensor()
        }
    }
    EndContact(contact: b2Contact<b2Shape, b2Shape>): void {

        const shape = contact.GetFixtureA().GetShape().GetType();
        if (shape == 1) {
            this.onJump = false;
        }

    }
    PreSolve(contact: b2Contact<b2Shape, b2Shape>, oldManifold: b2Manifold): void {

    }
    PostSolve(contact: b2Contact<b2Shape, b2Shape>, impulse: b2ContactImpulse): void {

    }

}